import os
from flask import Flask, jsonify
from flask_cors import CORS
from app import create_app, db
from app.models.setting import Setting
from app.models.focus_session import FocusSession
from app.models.goal import Goal
from app.models.statistic import Statistic
import json

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
CORS(app)

@app.shell_context_processor
def make_shell_context():
    return dict(db=db, Setting=Setting, FocusSession=FocusSession, 
                Goal=Goal, Statistic=Statistic, json=json)

# 初始化应用，确保数据库中有默认设置
def initialize_app():
    # 检查是否已有设置
    setting = Setting.query.first()
    if not setting:
        # 创建默认设置
        setting = Setting(
            focus_duration=25,
            short_break_duration=5,
            long_break_duration=15,
            long_break_interval=4,
            auto_start_break=True,
            auto_start_focus=False,
            notification='{"enabled": true, "sound": true}',
            sound='{"enabled": true, "volume": 80}',
            theme='{"mode": "light", "color": "blue"}'
        )
        db.session.add(setting)
        db.session.commit()

if __name__ == '__main__':
    # 在应用启动前初始化
    with app.app_context():
        initialize_app()
    app.run(debug=True) 